<?php
namespace app\controller\admin;

use app\BaseController;
use app\Request;
use think\facade\Db;
use think\Collection;

class Prize extends BaseController{
    public function index(Request $request)
    {
        $param = $request->param();
        $member = Db::name("scale_prize")
            ->alias('t1')
            ->join('scale_prize_box t2','t2.id=t1.prize_box_id');

        if(isset($param['prize_box_id']) && $param['prize_box_id'] !=''){
            $member = $member->where('t1.prize_box_id', $param['prize_box_id']);
        }
        if(isset($param['prize_name']) && !empty($param['prize_name'])){
            $member = $member->whereLike('t1.prize_name', '%'.$param['prize_name'].'%');
        }

        if(isset($param['rare']) && $param['rare'] !=''){
            $member = $member->where('t1.rare', $param['rare']);
        }
        if(isset($param['status']) && $param['status'] !=''){
            $member = $member->where('t1.status', $param['status']);
        }
        $member = $member
            ->where('t1.is_del', 0)
            ->field('t1.*,t2.prize_box_name')
            ->paginate($param['limit']);

        return api_json(20000,'请求数据成功',$member);
    }

    public function info(Request $request)
    {
        $param = $request->param();
        $member = Db::name("scale_prize")
                ->alias('t1')
                ->join('scale_prize_box t2','t2.id=t1.prize_box_id')
                ->where('id',$param['id'])
                ->select();
        return api_json(20000,'请求数据成功',$member);
    }

    public function changeStatus(Request $request)
    {
        $param = $request->param();
        $res = Db::name('scale_prize')
            ->where('id',$param['id'])
            ->data(['status'=>$param['status']])
            ->update();
        if($res){
            return api_json(20000,'操作成功',[]);
        }else{
            return api_json(0,'操作失败',[]);
        }
    }

    public function delPrize(Request $request)
    {
        $param = $request->param();
        $res = Db::name('scale_prize')
            ->where('id',$param['id'])
            ->data(['is_del'=>1])
            ->update();
        if($res){
            return api_json(20000,'操作成功',[]);
        }else{
            return api_json(0,'操作失败',[]);
        }
    }
    public function create(Request $request)
    {
        $param = $request->param();
        $data = [
          "prize_box_id" => $param['prize_box_id'],
          "prize_name" => $param['prize_name'],
          "chance" => $param['chance'],
          "rare" => $param['rare'],
          "status" => $param['status'],
        ];

        $res = Db::name('scale_prize')
            ->save($data);
        if($res){
            return api_json(20000,'保存成功',[]);
        }else{
            return api_json(0,'保存失败',[]);
        }
    }
    public function update(Request $request)
    {
        $param = $request->param();
        $data = [
          "id" => $param['id'],
          "prize_name" => $param['prize_name'],
          "chance" => $param['chance'],
          "rare" => $param['rare'],
          "status" => $param['status'],
        ];

        $res = Db::name('scale_prize')
            ->save($data);
        if($res){
            return api_json(20000,'保存成功',[]);
        }else{
            return api_json(0,'保存失败',[]);
        }
    }

    /**
     * 模拟抽奖
     */
    public function analog(Request $request)
    {
        $param = $request->param();
        $member = Db::name("scale_prize");

        if(isset($param['prize_box_id']) && $param['prize_box_id'] !=''){
            $member = $member->where('prize_box_id', $param['prize_box_id']);
        }
        $member = $member
            ->where('is_del', 0)
            ->where('status', 1)
            ->column('chance', 'id');
        $rest = $this->randomSelect($member,$param['prize_num']);
        return api_json(20000,'请求数据成功',$rest);
    }

    /**
     * 抽奖数据
     * @param $array
     * @param $num
     * @param array $numArr
     * @return string|void
     */
    public function randomSelect($array, $num, $numArr=[]){
        $datas = $array;
        if( !is_array($datas) || count($datas) == 0 )
            return ;
        asort($datas); //按照大小排序
        $proSum = array_sum($datas);
//        $random = rand(0,$proSum);
        $random = $this->randFloat(0,$proSum);
        $sum = 0 ;

        $flag = '';
        foreach($datas as $key => $data ){
            $sum += $data ;
            if( $random <= $sum ){
                $flag = $key;
                break ;
            }
        }
        $numArr[] = $flag;
        if($num > 1){
            $num--;
            return $this->randomSelect($array,$num,$numArr);
        }else{
            $ss_arr = [];
            $ss = array_count_values($numArr);
            foreach($ss as $k => $v){
//                $gift = Db::name("scale_prize")
//                    ->alias('t1')
//                    ->leftJoin('scale_prize_box t2','t2.id = t1.prize_box_id')
//                    ->field('t1.id,t1.prize_name,t1.rare,t2.price')
//                    ->where('t1.id', $k)
//                    ->find();
                $gift = Db::name("scale_prize")
                    ->field('id,prize_name,rare')
                    ->find($k);
                $gift['num'] = $v;
                $ss_arr[] = $gift;
            }
            return $ss_arr;
        }
    }

    /**
     * 生成0~1随机小数
     * @param Int  $min
     * @param Int  $max
     * @return Float
     */
    public function randFloat($min=0, $max=100){
        $format_num = $min + mt_rand()/mt_getrandmax() * ($max-$min);
        return sprintf("%.3f",$format_num);
    }
}